﻿Imports Microsoft.Msdn.Samples.BITS
Public Class FrmJob

    ''' <summary>
    ''' Indicates, that the job displayed has been deleted
    ''' </summary>
    ''' <remarks></remarks>
    Public Event EVENT_JOBDELETED()

    Private Job As Job

    Public Const MODE_DISPLAY As Byte = 1
    Public Const MODE_EDIT As Byte = 2

    Private Sub FrmJob_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Select Case My.Settings.Refresh
            Case Is = 0
                TimRefresh.Enabled = False
            Case Is = 300
                TimRefresh.Interval = 300
                TimRefresh.Enabled = True
            Case Is = 800
                TimRefresh.Interval = 800
                TimRefresh.Enabled = True
            Case Is = 1500
                TimRefresh.Interval = 1500
                TimRefresh.Enabled = True
            Case Is = 3000
                TimRefresh.Interval = 3000
                TimRefresh.Enabled = True
            Case Else
                TimRefresh.Interval = My.Settings.Refresh
                TimRefresh.Enabled = True
        End Select
    End Sub
    ''' <summary>
    ''' Initializes and displays this form
    ''' </summary>
    ''' <param name="mode">Opens the form in display/change mode</param>
    ''' <param name="JobNumber">Item number in the JobCollection of modbits.bits</param>
    ''' <remarks></remarks>
    Public Sub _Show(ByVal mode As Byte, ByVal JobNumber As Integer)
        Dim Jobs As JobCollection
        Jobs = ModBITS.BITS.GetListofJobs
        Job = Jobs(JobNumber)
        _ReadJob()
        Me.Show()
    End Sub

    ''' <summary>
    ''' Enteres/Updates the information on this form
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub _ReadJob()


        Dim File As ListViewItem
        Dim i As Integer


        LblJobNameValue.Text = Job.DisplayName
        LblJobDescValue.Text = Job.Description
        LblCreatedValue.Text = Job.CreationTime.ToString
        LblLAstModifiedValue.Text = Job.ModificationTime
        If Job.State = JobState.Errors Or Job.State = JobState.TransientError Then
            LblErrorDescValue.Text = Job.GetError.Description & vbCrLf & "Bandyt BITS is trying to resume the download"
        Else
            LblErrorDescValue.Text = "Job is not in error state"
        End If
        Select Case Job.Priority
            Case Is = JobPriority.Foreground
                CmbPriority.SelectedIndex = 3
            Case Is = JobPriority.High
                CmbPriority.SelectedIndex = 2
            Case Is = JobPriority.Normal
                CmbPriority.SelectedIndex = 1
            Case Is = JobPriority.Low
                CmbPriority.SelectedIndex = 0
        End Select

        LblIdValue.Text = Job.ID.ToString
        LblOwnerValue.Text = Job.GetOwnerName
        LblOwnerSIDValue.Text = Job.OwnerSID
        LblCurrentStateValue.Text = Job.State.ToString
        

        LblBytesTransferred.Text = Strings.Format(Job.Progress.BytesTransferred, "##,##0")
        LblBytesTotal.Text = Strings.Format(Job.Progress.BytesTotal, "##,##0")
        PrgBytes.Minimum = 0
        'Try
        If Job.Progress.BytesTotal > 2147483647 Then
            Dim IntFactor As New UInt32
            IntFactor = Math.Ceiling(Job.Progress.BytesTotal / 2147483647)
            PrgBytes.Maximum = Math.Ceiling(Job.Progress.BytesTotal / IntFactor)
            PrgBytes.Value = Math.Ceiling(Job.Progress.BytesTransferred / IntFactor)
        Else
            PrgBytes.Maximum = Job.Progress.BytesTotal
            PrgBytes.Value = Convert.ToInt32(Job.Progress.BytesTransferred)
        End If

        LblFilesTotal.Text = Job.Progress.FilesTotal.ToString
        LblFilesTransferred.Text = Job.Progress.FilesTransferred.ToString
        PrgFiles.Minimum = 0
        PrgFiles.Maximum = Job.Progress.FilesTotal
        PrgFiles.Value = Job.Progress.FilesTransferred
        If Job.Files.Count <> LViewFiles.Items.Count Then
            For i = 0 To Job.Files.Count - 1
                File = New ListViewItem
                File.Text = Job.Files(i).LocalName
                File.SubItems.Add(Job.Files(i).RemoteName)
                If Job.Files(i).Progress.BytesTotal < 1000000000000000000 Then
                    File.SubItems.Add(Strings.Format(Job.Files(i).Progress.BytesTransferred, "##,##0"))
                    File.SubItems.Add(Strings.Format(Job.Files(i).Progress.BytesTotal, "##,##0"))
                    File.SubItems.Add(Strings.Format((Job.Files(i).Progress.BytesTransferred / Job.Files(i).Progress.BytesTotal), "##0.00%"))
                Else
                    File.SubItems.Add("?")
                    File.SubItems.Add("?")
                    File.SubItems.Add(String.Format(0, "##0.00%"))
                End If
                LViewFiles.Items.Add(File)
            Next
        Else
            For i = 0 To Job.Files.Count - 1
                LViewFiles.Items(i).Text = Job.Files(i).LocalName
                LViewFiles.Items(i).SubItems(1).Text = Job.Files(i).RemoteName
                If Job.Files(i).Progress.BytesTotal < 1000000000000000000 Then

                    LViewFiles.Items(i).SubItems(2).Text = (Strings.Format(Job.Files(i).Progress.BytesTransferred, "##,##0"))
                    LViewFiles.Items(i).SubItems(3).Text = (Strings.Format(Job.Files(i).Progress.BytesTotal, "##,##0"))
                    LViewFiles.Items(i).SubItems(4).Text = Strings.Format((Job.Files(i).Progress.BytesTransferred / Job.Files(i).Progress.BytesTotal), "##0.00%")
                Else
                    LViewFiles.Items(i).SubItems(2).Text = ("?")
                    LViewFiles.Items(i).SubItems(3).Text = ("?")
                    LViewFiles.Items(i).SubItems(4).Text = (Strings.Format(0, "##0.00%"))
                End If
            Next
        End If

    End Sub

    Private Sub MnuJobExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuJobExit.Click
        Me.Close()
    End Sub

    Private Sub CmdResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdResume.Click
        Try
            Job.ResumeJob()
        Catch ex As Exception
            MsgBox("Error resuming job", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Cannot Resume Job")
        Finally
            _ReadJob()
        End Try
    End Sub

    Private Sub CmdSuspend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSuspend.Click
        Try
            Job.Suspend()
        Catch ex As Exception
            MsgBox("Error suspending job", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Cannot Suspend Job")
        Finally
            _ReadJob()
        End Try
    End Sub

    Private Sub MnuViewUpdateNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdateNow.Click
        _ReadJob()
    End Sub

    Private Sub MnuViewUpdatespeedVeryHigh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdatespeedVeryHigh.Click
        TimRefresh.Interval = 300
        TimRefresh.Start()
    End Sub

    Private Sub TimRefresh_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimRefresh.Tick
        _ReadJob()
    End Sub

    Private Sub MnuViewUpdatespeedHigh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdatespeedHigh.Click
        TimRefresh.Interval = 800
        TimRefresh.Start()
    End Sub

    Private Sub MnuViewUpdatespeedNormal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdatespeedNormal.Click
        TimRefresh.Interval = 1500
        TimRefresh.Start()
    End Sub

    Private Sub MnuViewUpdatespeedSlow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdatespeedSlow.Click
        TimRefresh.Interval = 3000
        TimRefresh.Start()
    End Sub

    Private Sub MnuViewUpdatespeedNever_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdatespeedNever.Click
        TimRefresh.Stop()
    End Sub

    Private Sub CmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdDelete.Click
        Dim answer As MsgBoxResult
        answer = MsgBox("Are you sure, that you want to delete this job? Any progress will be lost.", MsgBoxStyle.Critical + MsgBoxStyle.YesNo, "Delete Job?")
        If answer = MsgBoxResult.No Then
            Exit Sub
        Else
            Job.Cancel()
            RaiseEvent EVENT_JOBDELETED()
            Me.Close()
        End If
    End Sub

    Private Sub CmdTakeOwnership_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdTakeOwnership.Click
        Try
            Job.TakeOwnership()
        Catch ex As Exception
            MsgBox("Error taking Ownership. Please if you have the permission to take over this job.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Taking Ownership")
        Finally
            _ReadJob()
        End Try
    End Sub

    Private Sub MnuJobResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuJobResume.Click
        CmdResume_Click(sender, e)
    End Sub

    Private Sub MnuJobSuspend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuJobSuspend.Click
        CmdSuspend_Click(sender, e)
    End Sub

    Private Sub DeleteJobToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteJobToolStripMenuItem.Click
        CmdDelete_Click(sender, e)
    End Sub

    
    Private Sub CmnuFilesProperties_Click(sender As Object, e As EventArgs) Handles CmnuFilesProperties.Click

    End Sub
End Class